package com._21cn.cms.template.utils.label;

import com._21cn.cms.site.ConfigKeys;
import com._21cn.cms.site.entity.Channel;
import com._21cn.cms.site.entity.Config;
import com._21cn.cms.site.entity.Region;
import com._21cn.cms.site.service.ChannelService;
import com._21cn.cms.site.service.ConfigService;
import com._21cn.cms.site.service.RegionService;
import com._21cn.cms.template.utils.PageTurnUtil;
import com._21cn.framework.utils.DateUtil;
import com._21cn.framework.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * Created by Thinkpad on 2017/4/22.
 */
@Component( "toolsLabel" )
public class ToolsLabel {

    @Autowired
    private RegionService regionService;

    @Autowired
    private ConfigService configService;

    @Autowired
    private ChannelService channelService;

    public static String formatDate(Date date, String format){
        String str = DateUtil.formatDate(date, format);
        return str;
    }

    public static String formatTimestamp10(Integer timestamp, String format) {
        if (timestamp == null) return "";
        Date date = new Date(Long.valueOf(timestamp.intValue()).longValue() * 1000L);
        String str = DateUtil.formatDate(date, format);
        return str;
    }

    public String regionInclude(int id, int templateId){
        if( id>0 ){
            Region region = regionService.getById(id);
            if( region!=null && !StringUtil.isEmpty(region.getUrl()) ){
                return region.getUrl().replace("{templateId}",templateId+"");
            }
        }
        return "";
    }

    public static String getPageNavStr(String currentLink, int pageSize, int count){
        currentLink = StringUtil.html2Text(currentLink, "");
        Integer totalPage = PageTurnUtil.getTotalPage(Integer.valueOf(pageSize), Integer.valueOf(count));
        String[] pageNavList = PageTurnUtil.getPageNav(currentLink, totalPage.intValue());
        String pageNavStr = pageNavList[0] + pageNavList[2] + pageNavList[1];
        return pageNavStr;
    }

    public static String getPageNavStr(String currentLink, int pageSize, int count, String permParams) {
        currentLink = StringUtil.html2Text(currentLink, "");
        String[] permAry = permParams.split(",");
        List permList = Arrays.asList(permAry);
        String[] strAry = currentLink.split("\\?");
        if (strAry.length > 1) {
            Map paramMap = new HashMap();
            String paramStr = strAry[1];
            String[] paramAry = paramStr.split("&");
            if (paramAry.length > 0) {
                for (String param : paramAry) {
                    String[] ary = param.split("=");
                    if ((ary.length > 1) &&
                            (permList.contains(ary[0]))) {
                        paramMap.put(ary[0], ary[1]);
                    }
                }
            }

            currentLink = combineUrl(strAry[0], paramMap);
        }

        return getPageNavStr(currentLink, pageSize, count);
    }

    private static String combineUrl(String url, Map<String, String> params) {
        url = url + "?";
        for (String key : params.keySet()) {
            url = url + key + "=" + (String)params.get(key) + "&";
        }
        url = url.substring(0, url.length() - 1);
        return url;
    }

    public static String formatPicUrl( String picUrl , String code ) {
        String[] picUrls = picUrl.split("\\?");
        picUrl = picUrls[0];
        if (StringUtil.isImageFile(picUrl)) {
            try {
                String[] thumbAry = picUrl.split("/");
                String imgSize = thumbAry[(thumbAry.length - 2)];
                picUrl = picUrl.replaceFirst("/" + imgSize + "/", "/" + code + "/");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return picUrl;
    }

    public static <E> List<E> range(List<E> list, int position) {
        return range(list, position, position);
    }

    public static <E> List<E> range(List<E> list, int begin, int end) {
        List result = new ArrayList();
        if ((list == null) || (list.isEmpty()) || (begin > end)) {
            return result;
        }
        begin -= 1;
        int size = list.size();
        begin = begin > size ? size : begin < 0 ? 0 : begin;
        end = end > size ? size : end < 0 ? size : end;

        for (int i = begin; i < end; i++) {
            result.add(list.get(i));
        }
        return result;
    }

}
